<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Origami::Filter::Utils::BitWriter</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Origami::Filter::Utils::BitWriter</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/origami/filters_rb.html">
                origami/filters.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Class used to forge a <a href="../../String.html">String</a> from a stream
of bits. Internally used by some filters.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000334">final</a>&nbsp;&nbsp;
      <a href="#M000331">new</a>&nbsp;&nbsp;
      <a href="#M000333">size</a>&nbsp;&nbsp;
      <a href="#M000335">to_s</a>&nbsp;&nbsp;
      <a href="#M000332">write</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000331" class="method-detail">
        <a name="M000331"></a>

        <div class="method-heading">
          <a href="#M000331" class="method-signature">
          <span class="method-name">new</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000331-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000331-source">
<pre>
    <span class="ruby-comment cmt"># File origami/filters.rb, line 43</span>
43:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>
44:           <span class="ruby-ivar">@data</span> = <span class="ruby-value str">''</span>
45:           <span class="ruby-ivar">@last_byte</span> = <span class="ruby-keyword kw">nil</span>
46:           <span class="ruby-ivar">@ptr_bit</span> = <span class="ruby-value">0</span>
47:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000334" class="method-detail">
        <a name="M000334"></a>

        <div class="method-heading">
          <a href="#M000334" class="method-signature">
          <span class="method-name">final</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Finalizes the stream.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000334-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000334-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters.rb, line 99</span>
 99:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">final</span>
100:           <span class="ruby-ivar">@data</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@last_byte</span>.<span class="ruby-identifier">chr</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@last_byte</span>
101:           <span class="ruby-ivar">@last_byte</span> = <span class="ruby-keyword kw">nil</span>
102:           <span class="ruby-ivar">@p</span> = <span class="ruby-value">0</span>
103: 
104:           <span class="ruby-keyword kw">self</span>
105:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000333" class="method-detail">
        <a name="M000333"></a>

        <div class="method-heading">
          <a href="#M000333" class="method-signature">
          <span class="method-name">size</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the data <a href="BitWriter.html#M000333">size</a> in bits.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000333-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000333-source">
<pre>
    <span class="ruby-comment cmt"># File origami/filters.rb, line 92</span>
92:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">size</span>
93:           (<span class="ruby-ivar">@data</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">3</span>) <span class="ruby-operator">+</span> <span class="ruby-ivar">@ptr_bit</span>
94:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000335" class="method-detail">
        <a name="M000335"></a>

        <div class="method-heading">
          <a href="#M000335" class="method-signature">
          <span class="method-name">to_s</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Outputs the stream as a <a href="../../String.html">String</a>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000335-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000335-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters.rb, line 110</span>
110:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_s</span>
111:           <span class="ruby-ivar">@data</span>.<span class="ruby-identifier">dup</span>
112:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000332" class="method-detail">
        <a name="M000332"></a>

        <div class="method-heading">
          <a href="#M000332" class="method-signature">
          <span class="method-name">write</span><span class="method-args">(data, length)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Writes <em>data</em> represented as Fixnum to a <em>length</em> number of
bits.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000332-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000332-source">
<pre>
    <span class="ruby-comment cmt"># File origami/filters.rb, line 52</span>
52:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">write</span>(<span class="ruby-identifier">data</span>, <span class="ruby-identifier">length</span>)
53:           <span class="ruby-keyword kw">return</span> <span class="ruby-constant">BitWriterError</span>, <span class="ruby-value str">&quot;Invalid data length&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-keyword kw">and</span> (<span class="ruby-value">1</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">length</span>) <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">data</span>
54: 
55:           <span class="ruby-comment cmt"># optimization for aligned byte writing</span>
56:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">8</span> <span class="ruby-keyword kw">and</span> <span class="ruby-ivar">@last_byte</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">and</span> <span class="ruby-ivar">@ptr_bit</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
57:             <span class="ruby-ivar">@data</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">chr</span>
58:             <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">self</span>
59:           <span class="ruby-keyword kw">end</span>
60: 
61:           <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
62:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">8</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@ptr_bit</span>
63:               <span class="ruby-identifier">length</span> <span class="ruby-operator">-=</span> <span class="ruby-value">8</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@ptr_bit</span>
64:               <span class="ruby-ivar">@last_byte</span> = <span class="ruby-value">0</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@last_byte</span>
65:               <span class="ruby-ivar">@last_byte</span> <span class="ruby-operator">|=</span> (<span class="ruby-identifier">data</span> <span class="ruby-operator">&gt;&gt;</span> <span class="ruby-identifier">length</span>) <span class="ruby-operator">&amp;</span> ((<span class="ruby-value">1</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-value">8</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@ptr_bit</span>)) <span class="ruby-operator">-</span> <span class="ruby-value">1</span>)
66: 
67:               <span class="ruby-identifier">data</span> <span class="ruby-operator">&amp;=</span> (<span class="ruby-value">1</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">length</span>) <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
68:               <span class="ruby-ivar">@data</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@last_byte</span>.<span class="ruby-identifier">chr</span>
69:               <span class="ruby-ivar">@last_byte</span> = <span class="ruby-keyword kw">nil</span>
70:               <span class="ruby-ivar">@ptr_bit</span> = <span class="ruby-value">0</span>
71:             <span class="ruby-keyword kw">else</span>
72:               <span class="ruby-ivar">@last_byte</span> = <span class="ruby-value">0</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@last_byte</span>
73:               <span class="ruby-ivar">@last_byte</span> <span class="ruby-operator">|=</span> (<span class="ruby-identifier">data</span> <span class="ruby-operator">&amp;</span> ((<span class="ruby-value">1</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">length</span>) <span class="ruby-operator">-</span> <span class="ruby-value">1</span>)) <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-value">8</span> <span class="ruby-operator">-</span> <span class="ruby-ivar">@ptr_bit</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">length</span>)
74:               <span class="ruby-ivar">@ptr_bit</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">length</span>
75:               
76:               <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@ptr_bit</span> <span class="ruby-operator">==</span> <span class="ruby-value">8</span>
77:                 <span class="ruby-ivar">@data</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@last_byte</span>.<span class="ruby-identifier">chr</span>
78:                 <span class="ruby-ivar">@last_byte</span> = <span class="ruby-keyword kw">nil</span>
79:                 <span class="ruby-ivar">@ptr_bit</span> = <span class="ruby-value">0</span>
80:               <span class="ruby-keyword kw">end</span>
81:               
82:               <span class="ruby-identifier">length</span> = <span class="ruby-value">0</span>
83:             <span class="ruby-keyword kw">end</span>
84:           <span class="ruby-keyword kw">end</span>
85: 
86:           <span class="ruby-keyword kw">self</span>
87:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>